package controllers;

import java.io.File;
import java.util.List;

import models.Article;
import models.ArticleMulti;
import models.Clob;
import models.Reply;
import net.ufrog.common.utils.Strings;

import org.apache.commons.lang.StringEscapeUtils;

import play.Logger;
import utils.Page;
import utils.dict.Dicts;
import utils.json.serializer.ArticleSerializer;
import utils.json.serializer.PageSerializer;

/**
 * 素材控制器
 * 
 * @author ultrafrog
 * @version 1.0, 2014-11-13
 * @since 1.0
 */
public class Resources extends Admin {

	/** 素材管理 */
	public static void index() {
		render();
	}
	
	/** 单图文信息 */
	public static void article() {
		render();
	}
	
	/**
	 * 单图文信息查询
	 * 
	 * @param title
	 * @param page
	 * @param size
	 */
	public static void article_search(Integer page, Integer size) {
		// 查询数据
		Page<Article> p = new Page<Article>(page, size);
		Long total = Article.count();
		List<Article> articles = Article.find("order by datetime desc").fetch(page, size);
		
		// 设置数据
		article_parse(articles);
		p.setTotal(total);
		p.getContent().addAll(articles);
		
		// 输出数据
		renderJSON(p, new PageSerializer(), new ArticleSerializer());
	}
	
	/**
	 * 单图文信息保存
	 * 
	 * @param id
	 * @param title
	 * @param author
	 * @param description
	 * @param picUrl
	 * @param text
	 */
	public static void article_save(String id, String title, String author, String description, String picUrl, String text) {
		Article article = Strings.empty(id) ? new Article() : Article.class.cast(Article.findById(id));
		article.save(title, author, description, picUrl, Clob.save(article.text, StringEscapeUtils.escapeHtml(text)));
		renderJSON(article);
	}
	
	/**
	 * 单图文信息删除
	 * 
	 * @param id
	 */
	public static void article_delete(String id) {
		Article article = Article.findById(id);
		if (article != null) {
			Clob clob = Clob.findById(article.text);
			clob.delete();
			article.delete();
		}
	}
	
	/** 多图文信息 */
	public static void article_multi() {
		render();
	}
	
	/**
	 * 多图文信息查询
	 * 
	 * @param page
	 * @param size
	 */
	public static void article_multi_search(Integer page, Integer size) {
		// 查询数据
		Page<ArticleMulti> p = new Page<ArticleMulti>(page, size);
		Long total = ArticleMulti.count();
		List<ArticleMulti> articleMultis = ArticleMulti.find("order by datetime desc").fetch(page, size);
		
		// 设置数据
		for (ArticleMulti am: articleMultis) {
			article_parse(am.getArticles());
		}
		p.setTotal(total);
		p.getContent().addAll(articleMultis);
		
		// 输出数据
		renderJSON(p, new PageSerializer(), new ArticleSerializer());
	}
	
	/**
	 * 多图文信息保存
	 * 
	 * @param id
	 * @param articles
	 */
	public static void article_multi_save(String id, String articles) {
		ArticleMulti am = Strings.empty(id) ? new ArticleMulti() : ArticleMulti.class.cast(ArticleMulti.findById(id));
		am.articleIds = articles;
		am.save();
		
		article_parse(am.getArticles());
		renderJSON(am, new ArticleSerializer());
	}
	
	/**
	 * 多图文信息删除
	 * 
	 * @param id
	 */
	public static void article_multi_delete(String id) {
		ArticleMulti articleMulti = ArticleMulti.findById(id);
		if (articleMulti != null) {
			articleMulti.delete();
		}
	}
	
	/**
	 * 图文信息弹出层
	 * 
	 * @param type
	 */
	public static void article_popover(String type, String callback) {
		renderArgs.put("typeName", Dicts.name(type, Reply.Type.class));
		renderArgs.put("type", type);
		renderArgs.put("callback", callback);
		render();
	}
	
	/**
	 * 图文信息读取
	 * 
	 * @param id
	 * @param type
	 */
	public static void article_get(String id, String type) {
		if (Strings.equals(Reply.Type.SINGLE, type)) {
			Article article = Article.findById(id);
			renderJSON(article, new ArticleSerializer());
		} else if (Strings.equals(Reply.Type.MULTI, type)) {
			ArticleMulti am = ArticleMulti.findById(id);
			article_parse(am.getArticles());
			renderJSON(am);
		}
		renderJSON(null);
	}
	
	/** 图片库 */
	public static void image() {
		render();
	}
	
	/**
	 * 图片查询
	 * 
	 * @param page
	 * @param size
	 */
	public static void image_search(Integer page, Integer size) {
		// 查询数据
		Object[] params = new Object[] {"png", "jpg", "jpeg", "bmp"};
		Page<models.File> p = new Page<models.File>(page, size);
		Long total = models.File.count("ext in (?, ?, ?, ?)", params);
		List<models.File> files = models.File.find("ext in (?, ?, ?, ?) order by createTime desc", params).fetch(page, size);
		
		// 设置数据
		p.setTotal(total);
		p.getContent().addAll(files);
		
		// 输出数据
		renderJSON(p, new PageSerializer());
	}
	
	/**
	 * 图片上传
	 * 
	 * @param file
	 */
	public static void image_upload(File file) {
		models.File f = saveFile(file, 900, 500, "png", "jpg", "jpeg", "bmp");
		Logger.info("%s: %s", f.name, f.path);
		renderJSON(f);
	}

	/**
	 * 图片删除
	 * 
	 * @param id
	 */
	public static void image_delete(String id) {
		models.File file = models.File.findById(id);
		if (file != null) {
			File f = new File(file.path);
			f.delete();
			file.delete();
		}
	}
	
	/**
	 * 图片库弹出层
	 * 
	 * @param callback
	 */
	public static void image_popover(String callback) {
		renderArgs.put("callback", callback);
		render();
	}

	/**
	 * @param article
	 */
	protected static void article_parse(Article article) {
		article.text = StringEscapeUtils.unescapeHtml(Clob.get(article.text));
	}
	
	/**
	 * @param articles
	 */
	protected static void article_parse(List<Article> articles) {
		for (Article article: articles) {
			article_parse(article);
		}
	}
}
